﻿#region Using directives

using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

using Weazel.Toad.Application.Items;
using Weazel.Toad.Application.Actions;

#endregion

namespace Weazel.Toad.Application.Controls
{
  [ToolboxItem(true)]
  public partial class ProjectExplorer : UserControl
  {
    public class ItemTreeNode : TreeNode
    {
      private ItemBase item;
      public ItemBase Item
      {
        get { return item; }        
      }

      public ItemTreeNode(ItemBase item)
        :
        base(item.DisplayName)
      {
        this.item = item;
        this.item.ItemDisplayNameChanged += new EventHandler<EventArgs>(item_ItemDisplayNameChanged);
        this.item.ItemFilenameChanged += new EventHandler<EventArgs>(item_ItemFilenameChanged);
      }

      void item_ItemFilenameChanged(object sender, EventArgs e)
      {
        Text = ((ItemBase)sender).DisplayName;
      }

      void item_ItemDisplayNameChanged(object sender, EventArgs e)
      {
        Text = ((ItemBase)sender).DisplayName;
      }
    }

    private Project project = null;

    public ProjectExplorer()
    {
      InitializeComponent();
      
      this.treeView.MouseUp +=new MouseEventHandler(treeView_MouseUp);

      Project.ProjectClosed += new EventHandler<EventArgs>(Project_ProjectClosed);
      Project.ProjectOpened += new EventHandler<EventArgs>(Project_ProjectOpened);
      Project.ProjectCreated +=new EventHandler<EventArgs>(Project_ProjectCreated);
    }

    public void BindActions()
    {
    }

    private void Project_ProjectClosed(object sender, EventArgs e)
    {
      Debug.Assert(project != null);

      this.project.ItemChanged -= new EventHandler<EventArgs>(project_ItemChanged);
      this.project = null;
      treeView.Nodes.Clear();
    }

    private void Project_ProjectOpened(object sender, EventArgs e)
    {
      Debug.Assert(project == null);
      
      this.project = sender as Project;
      this.project.ItemChanged += new EventHandler<EventArgs>(project_ItemChanged);

      addNodesForProject(project);
    }

    void Project_ProjectCreated(object sender, EventArgs e)
    {
      Debug.Assert(project == null);
      
      this.project = sender as Project;
      this.project.ItemChanged += new EventHandler<EventArgs>(project_ItemChanged);

      addNodesForProject(project);
    }

    private void addNodesForProject(Project project)
    {
      TreeNode root = new TreeNode(project.DisplayName);
      root.ImageIndex = 1;
      root.SelectedImageIndex = 1;

      treeView.SuspendLayout();

      foreach (ItemBase item in project.Items)
      {
        ItemTreeNode node = new ItemTreeNode(item);
        root.Nodes.Add(node);
      }

      treeView.Nodes.Add(root);
      treeView.ExpandAll();

      treeView.ResumeLayout();
    }

    /// <summary>
    /// TODO: this could be done more
    /// intelligently and efficiently
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void project_ItemChanged(object sender, EventArgs e)
    {
      treeView.Nodes.Clear();
      addNodesForProject(project);
    }

    private void treeView_AfterExpand(object sender, TreeViewEventArgs e)
    {
      //TreeNode node = sender as TreeNode;
      //if (node == gezelFolderNode || node == vhdlFolderNode)
      //{
      //  node.ImageIndex = 3; // open folder icon
      //}
    }

    private void treeView_AfterCollapse(object sender, TreeViewEventArgs e)
    {
      //TreeNode node = sender as TreeNode;
      //if (node == gezelFolderNode || node == vhdlFolderNode)
      //{
      //  node.ImageIndex = 2; // closed folder icon
      //}
    }

    void treeView_MouseUp(object sender, MouseEventArgs e)
    {
      Point cursor = new Point(e.X, e.Y);
      if (e.Button == MouseButtons.Right && e.Clicks == 1)
      {
        TreeNode node = treeView.GetNodeAt(cursor);
        if (node != null)
        {
          treeView.SelectedNode = node;
          if (node.ContextMenuStrip != null)
          {
            node.ContextMenuStrip.Show(treeView, cursor);
          }
        }
      }
      else if (e.Button == MouseButtons.Left)
      {
        // open and bring forward
        TreeNode node = treeView.GetNodeAt(cursor);
        if (node != null)
        {
          treeView.SelectedNode = node;

          if (node is ItemTreeNode)
          {
            Application.Instance.AddOpenItem(((ItemTreeNode)node).Item);
          }
        }
      }
    }
  }
}
